import UAParser from 'ua-parser-js';
import { useAuthStore } from '@/store';
import { isArray, isString } from '@/utils';

/** 获取设备信息 */
export function useDeviceInfo() {
  const parser = new UAParser();
  const result = parser.getResult();
  return result;
}
/**
 * 基于资源的按钮权限判断
 */
export function usePermission() {
	const auth = useAuthStore();

	function  hasPermission(permission) {
		const { btnPermission } = auth.userInfo;
		if (isArray(btnPermission)){
			return btnPermission.includes(permission);
		}
		return false;
	}

	return {
		hasPermission
	};
}
/** 权限判断 */
// export function usePermission() {
//   const auth = useAuthStore();
//
//   function hasPermission(permission: Auth.RoleType | Auth.RoleType[]) {
//     const { userRole } = auth.userInfo;
//
//     let has = userRole === 'super';
//     if (!has) {
//       if (isArray(permission)) {
//         has = (permission as Auth.RoleType[]).includes(userRole);
//       }
//       if (isString(permission)) {
//         has = (permission as Auth.RoleType) === userRole;
//       }
//     }
//     return has;
//   }
//
//   return {
//     hasPermission
//   };
// }
/** 基于角色的权限判断 */
export function useRolePermission() {
	const auth = useAuthStore();

	function hasRolePermission(permission: Auth.RoleType | Auth.RoleType[]) {
		const { userRole } = auth.userInfo;

		let has = userRole === 'super';
		if (!has) {
			if (isArray(permission)) {
				has = (permission as Auth.RoleType[]).includes(userRole);
			}
			if (isString(permission)) {
				has = (permission as Auth.RoleType) === userRole;
			}
		}
		return has;
	}

	return {
		hasRolePermission
	};
}


